package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.FoodDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Food;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service(interfaceClass = FoodService.class)
@Transactional
public class FoodServiceImpl implements FoodService{
    @Autowired
    private FoodDao foodDao;
    //分页查询
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        //        如果是条件查询，默认当前页为1，不然在第二页无法查询到
        if(queryString!=null&&!"".equals(queryString)){
            currentPage=1;
        }
        PageHelper.startPage(currentPage,pageSize);
        Page<Food> page=foodDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //    新增运动
    public void add(Food food) {
        foodDao.add(food);
    }

    //    根据id查找
    public Food findById(Integer id) {
        return foodDao.findById(id);
    }

    //编辑
    public void edit(Food food) {
        foodDao.edit(food);
    }

    //    删除
    public void deleteById(Integer id) {
        foodDao.deleteById(id);
    }
}
